<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>sysRole页面</title>
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
</head>
<body>


<table id="demo" lay-filter="test" >

</table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
    </div>
</script>
<script type="text/html" id="currentTableBar">

    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    <a class="layui-btn layui-btn-xs layui-btn " lay-event="updatePermission">权限编辑</a>
</script>
<script src="/lib/layui-v2.5.5/layui.js"></script>

<script>


    layui.use('table', function(){
        var table = layui.table,
        $ = layui.jquery;

        table.render({
            elem: '#demo'
            ,height: 312
            ,url: '/selectRoleList' //数据接口
            ,limit:2
            ,request: {
                limitName: 'rows' //每页数据量的参数名，默认：limit

            }
            ,toolbar: "#toolbarDemo"
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID', width:180, sort: true, fixed: 'left'}
                ,{field: 'name', title: '角色名', width:120}
                ,{field: 'description', title: '描述', width:300, sort: true}
                ,{field: 'createTime', title: '创建时间', width:200}
                ,{field: 'updateTime', title: '修改时间', width: 200}
                , {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}

            ]]
        });
        table.on('tool(test)', function (obj) {

            var data = obj.data //获得当前行数据
            //将当前行的id存储到本地session中
            layui.sessionData('editRole',{
                key:'role-id',
                value:data.id
            });
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改角色',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../role/update.html'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url: "/deleteRole",
                        type: "get",
                        data:{'id':obj.data.id},
                        success:function (data) {
                            console.log(data)
                           if (data.code==0){
                              location.reload();
                           }else{
                               layer.msg("删除失败");
                           }
                        }
                    })
                    layer.close(index);
                });
            }else if (obj.event === 'updatePermission'){//权限编辑

                location.href="2.html?roleName="+data.name;
            }

        });
    /*    $("#add").onclick(function () {
            $.ajax({
                url:"/addRole",
                type:"get",
                content: '/layui/sysRole/add.html'
            });
        })*/
        table.on('toolbar(test)', function (obj) {
            if(obj.event === 'add'){
                var index = layer.open({
                    title: '添加角色',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../role/add.html'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        })

    });

</script>

</body>
</html>